﻿using System;
using System.IO;
using System.Text;
using Tsanie.ShellExt.Definitions;

namespace Tsanie.ShellExt.InfoTip
{
    public class TextProvider : InfoTipBaseProvider
    {
        public override string ProviderName { get { return "InfoTip.Text"; } }
        public override ProviderType Type { get { return ProviderType.InfoTip; } }
        public override string[] Exts { get { return new[] { ".txt" }; } }
        public override string Creator { get { return "Tsanie"; } }

        public override string GetInfoTip(string filename, InfoTipOptions options)
        {
            StringBuilder sb = new StringBuilder(0x40);
            sb.AppendLine("Type: Text Document");
            FileInfo fi = new FileInfo(filename);
            sb.AppendLine("Size: " + fi.Length + " bytes");
            sb.Append("Intro:");
            using (StreamReader reader = new StreamReader(fi.OpenRead()))
            {
                try
                {
                    int count = 0;
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = " " + line.Trim();
                        if (count + line.Length > 30)
                        {
                            sb.Append(line.Substring(0, 30 - count));
                            break;
                        }
                        sb.Append(line);
                        count += line.Length;
                    }
                }
                catch (Exception e)
                {
                    Common.OutputError(string.Format("GetInfoTip('{0}', {1})", filename, options), e);
                }
                finally
                {
                    reader.Close();
                }
            }
            return sb.ToString();
        }
    }
}
